package com.tpyule.tpclass.service.impl;

import com.tpyule.tpclass.dao.SortDao;
import com.tpyule.tpclass.entity.bean.FailResultBean;
import com.tpyule.tpclass.entity.bean.ResultBean;
import com.tpyule.tpclass.entity.bean.SuccessResultBean;
import com.tpyule.tpclass.entity.bo.SortDetailBo;
import com.tpyule.tpclass.entity.dto.CommonQueryDto;
import com.tpyule.tpclass.entity.dto.CourseParamDto;
import com.tpyule.tpclass.entity.pojo.Course;
import com.tpyule.tpclass.entity.pojo.Sort;
import com.tpyule.tpclass.service.CourseService;
import com.tpyule.tpclass.service.SortService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author zzz
 * @date 2020/2/26 0026 下午 3:43
 */
@Service
public class SortServiceImpl implements SortService {
    @Autowired
    SortDao sortDao;
    @Autowired
    CourseService courseService;
    @Override
    public List<Sort> getAllSort() {
        List<Sort> sorts=sortDao.getAllSort();
        return sorts;
    }

    @Override
    public List<Sort> getSortByDirectionId(int directionId) {
        return sortDao.getSortByDirectionId(directionId);
    }

    @Override
    public int getSizeSort() {
        return sortDao.getSizeSort();
    }

    @Override
    public int getSizeSortByDirection(int directionId) {
        return sortDao.getSizeSortByDirection(directionId);
    }

    @Override
    public List<SortDetailBo> getListSort(CommonQueryDto commonQueryDto) {
        commonQueryDto.setStart((commonQueryDto.getPage()-1)*commonQueryDto.getPageSize());
        return sortDao.getListSort(commonQueryDto);
    }

    @Override
    public int updateSort(Sort sort) {
        return sortDao.updateSort(sort);
    }

    @Override
    public int addSort(Sort sort) {
        return sortDao.addSort(sort);
    }

    @Override
    public ResultBean deleteSort(Sort sort) {
        ResultBean resultBean = null;
        if (sort!=null){
            CourseParamDto courseParamDto = new CourseParamDto();
            courseParamDto.setSortId(sort.getSortId());
            int courseSize = courseService.getSizeCourse(courseParamDto);
            if(courseSize!=0){
                resultBean = new FailResultBean(502,"该分类下还有课程");
            }else{
                int result = sortDao.deleteSort(sort);
                if(result>0){
                    resultBean = new SuccessResultBean();
                } else {
                    resultBean = new FailResultBean("删除失败，请重试！");
                }
            }
        } else {
            resultBean = new FailResultBean("数据错误");
        }
        return resultBean;
    }


}
